package functions

import (
	"os"
	"os/exec"
)

// DoShell 同步调用命令行程序的代码
func DoShell(AValue string) (AOut string, APid int, AEr error) {
	cmd := exec.Command("/bin/sh", "-c", AValue) //linux
	mPid := cmd.Process.Pid
	//cmd := exec.Command("cmd", Avalue)
	out, err := cmd.Output()
	if err == nil {
		return string(out), mPid, nil
	} else {
		return "", -1, err
	}
}

// SystemCall 异步运行系统进程
func SystemCall(AExecutable string, Args []string) (Pid int, Er error) {
	attr := &os.ProcAttr{
		Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
	}
	mP, mErr := os.StartProcess(AExecutable, Args, attr)
	if mErr == nil {
		return mP.Pid, nil
	} else {
		return -1, mErr
	}
}
